using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class CameraCtrl : MonoBehaviour
{
    public GameObject _tgt;
    public GameObject _pitchCtrl;
    public float _dftSpd;
    public float _minAng;
    public float _maxAng;
    private Vector2 _ori;
    private float _spd;

    void Start()
    {
        _ori = Vector2.zero;
    }

    void Update()
    {
        ChaseTgt();
        Rot();
    }

    private void ChaseTgt()
    {
        transform.position = _tgt.transform.position;
    }

    public void GetOri(InputAction.CallbackContext context)
    {
        _ori = context.ReadValue<Vector2>();
        if (_ori == Vector2.zero) _spd = 0;
        else _spd = _dftSpd;
    }

    private void Rot()
    {
        if (_ori.y <= _ori.x && _ori.y >= -_ori.x)
        {
            transform.rotation = Quaternion.AngleAxis(_spd, Vector3.up) * transform.rotation;
            GameManager.Instance._curCameraOri = transform.rotation;
        }
        else if (_ori.y >= _ori.x && _ori.y <= -_ori.x)
        {
            transform.rotation = Quaternion.AngleAxis(-_spd, Vector3.up) * transform.rotation;
            GameManager.Instance._curCameraOri = transform.rotation;
        }

        if (_pitchCtrl.transform.rotation.eulerAngles.x < _maxAng)
        {
            if (_ori.y > -_ori.x && _ori.y > _ori.x) _pitchCtrl.transform.localRotation = Quaternion.AngleAxis(_spd, Vector3.right) * _pitchCtrl.transform.localRotation;
        }
        if (_pitchCtrl.transform.rotation.eulerAngles.x > _minAng)
        {
            if (_ori.y < _ori.x && _ori.y < -_ori.x) _pitchCtrl.transform.localRotation = Quaternion.AngleAxis(-_spd, Vector3.right) * _pitchCtrl.transform.localRotation;
        }
    }
}
